package cn.yusei.teamTribe.controller.team;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.service.team.TeamVideoService;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class TeamVideoController {
	
	@Autowired
	private TeamVideoService teamVideoService;
	@Value("${fileNameLength}")
	private int fileNameLength;

	@RequestMapping(value="/teamVideo/add")
	public void add(
			@RequestAttribute int id,
			@RequestParam int  teamId,
			@RequestParam int albumId,
			@RequestParam String fileName,
			PrintWriter out){
		if(teamId <= 0 || albumId <= 0 || fileName.length() != fileNameLength){
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try{
			code = teamVideoService.add(teamId, albumId, id, fileName);
		}
		catch (Exception e){
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
	@RequestMapping(value="/teamVideo/delete")
	public void delete(
			@RequestAttribute int id,
			@RequestParam int  teamId,
			@RequestParam int albumId,
			@RequestParam String fileName,
			PrintWriter out){
		if(teamId <= 0 || albumId <= 0 || fileName.length() != fileNameLength){
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try{
			code = teamVideoService.delete(teamId, albumId, id, fileName);
		}
		catch (Exception e){
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
	@RequestMapping(value="/teamVideo/get")
	public void get(@RequestParam int  teamId,
			@RequestParam int albumId,
			@RequestParam int offset,
			@RequestParam int num,
			PrintWriter out){
		if(teamId <= 0 || albumId <= 0 || offset < 0 || num <= 0){
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		String[] files = null;
		try{
			files = teamVideoService.get(teamId, albumId, offset, num);
		}
		catch (Exception e){
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("files", files);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
}
